//
//  CinemaViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaCell.h"
#import "CinemaHeaderView.h"

#import "CinemaDistritModel.h"
#import "CinemaModel2.h"

@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *flagArray;
    BOOL flag;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *cinemaDataArray;
@property (nonatomic, strong) NSMutableArray *distritDataArray;
@property (nonatomic, strong) NSMutableArray *rebulidArray;

@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    flag = YES;
    NSNumber *number = [NSNumber numberWithBool:flag];
    
    NSArray *array = @[number, number, number,number, number, number,number, number, number,number, number, number,number, number, number,number, number, number,number ];
    flagArray = [NSMutableArray arrayWithArray:array];
    
    [self _loadData];
    [self _rebulid];
    
    // Do any additional setup after loading the view.
}

- (void)_loadData {
    _distritDataArray = [NSMutableArray array];
    NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"district_list.json" ofType:nil ]] options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dictionary[@"districtList"];
    for (NSDictionary *dic in array) {
        CinemaDistritModel *model = [[CinemaDistritModel alloc] init];
        model.distritName = dic[@"name"];
        model.tag = [dic[@"id"] integerValue];
        [_distritDataArray addObject:model];
    }
    
    
    _cinemaDataArray = [NSMutableArray array];
    NSDictionary *dictionaty1 = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cinema_list.json" ofType:nil ]] options:NSJSONReadingMutableContainers error:nil];
    NSArray *array1 = dictionaty1[@"cinemaList"];
    for (NSDictionary *dic in array1) {
       // CinemaModel *model1 = [[CinemaModel alloc] init];
        CinemaModel2 *model = [[CinemaModel2 alloc] init];
//        model.name = dic[@"name"];
//        NSLog(@"%@", dic[@"name"]) ;
        model.name = dic[@"name"];
        model.grade = dic[@"grade"];
        model.address = dic[@"address"];
        model.lowPrice = dic[@"lowPrice"];
        model.tag = [dic[@"districtId"] integerValue];
        
        
        [_cinemaDataArray addObject:model];
    }
   
}

- (void)_rebulid {
    
    
    _rebulidArray = [NSMutableArray array];
    
    for (int i = 0; i < _distritDataArray.count; i++) {
        NSMutableArray *array = [NSMutableArray array];
        CinemaDistritModel *model = [[CinemaDistritModel alloc] init];
        model = _distritDataArray[i];
        NSInteger tag = model.tag;
        for (int j = 0; j < _cinemaDataArray.count; j++) {
            CinemaModel2 *model1 = [[CinemaModel2 alloc] init];
            model1 = _cinemaDataArray[j];
            
            if ([model1 tag] == tag) {
                [array addObject:model1];
            }
        }
     
        NSArray *array1 = [array copy];
        
        [_rebulidArray addObject:array1];
        [array removeAllObjects];
}
    
    
    
//    _rebulidArray = [NSMutableArray array];
//    
//    for (int i = 0 ; i < _distritDataArray.count ; i++) {
//        NSInteger number;
//        CinemaDistritModel *model = _distritDataArray[i];
//        NSInteger tag = model.tag;
//        for (int j = 0; j < _cinemaDataArray.count; j ++) {
//            CinemaModel *model1 = _cinemaDataArray[j];
//            if (model1.tag == tag) {
//                <#statements#>
//            }
//        }
//    }
//        
//    }
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    CinemaHeaderView *view = [[CinemaHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    view.model = _distritDataArray[section];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = view.frame;
    button.tag = section;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    return view;
    
}

- (void)buttonAction:(UIButton *)button {
    
    //button.selected = !button.selected;
    NSInteger tag =  button.tag;
    
    NSNumber * number =  flagArray[tag];
    flag = [number boolValue];
    flag = !flag;
    number = [NSNumber numberWithBool:flag];
    [flagArray replaceObjectAtIndex:tag withObject:number];
    

    [_tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    
    return  _distritDataArray.count;
        
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        NSNumber *number = flagArray[section];
        flag = [number boolValue];
        if (flag) {
            return 0;
        }else {
            return [_rebulidArray[section] count];
        }
  
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaCell" forIndexPath:indexPath];
    CinemaModel2 *model = [[CinemaModel2 alloc] init];
    NSArray *array = [NSArray arrayWithArray:_rebulidArray[indexPath.section]];
    model = array[indexPath.row];
    cell.model = model;
    
    
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
   
    CGSize size = [cell.cinemaLabel.text sizeWithAttributes:dic];
//    CGRect rect = [cell.cinemaLabel.text boundingRectWithSize:CGSizeMake(999, 999) options:<#(NSStringDrawingOptions)#> attributes:<#(nullable NSDictionary<NSString *,id> *)#> context:<#(nullable NSStringDrawingContext *)#>]
    //NSLog(@"%@",NSStringFromCGSize(size));
    
    CGFloat x = CGRectGetMaxX(cell.cinemaLabel.bounds);
    
    cell.cinemaLabel.frame = CGRectMake(0, 0, size.width, size.height);
    cell.gradeLabel.frame = CGRectMake( x + 40 , 10, 40, 30);
    
  
   
    return cell;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
